//
//  MCMMCNewViewController.m
//  TLYL
//
//  Created by MC on 2017/7/17.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCMMCNewViewController.h"
#import "UIView+MCParentController.h"
#import "MCPaySelectedLotteryTableViewController.h"
#import "MCBetModel.h"
#import "MCUserMoneyModel.h"

@interface MCMMCNewViewController ()

@property (nonatomic,strong)NSTimer * timer;

@property (nonatomic,strong)UIButton * btn_ball1;

@property (nonatomic,strong)UIButton * btn_ball2;

@property (nonatomic,strong)UIButton * btn_ball3;

@property (nonatomic,strong)UIButton * btn_ball4;

@property (nonatomic,strong)UIButton * btn_ball5;

@property (nonatomic,weak)UILabel  * lab_title;

@property (nonatomic,assign)int allKaiJiangCount;

@property (nonatomic,strong)UILabel * lab_back;

@property (nonatomic,strong)UIButton * btn_Stop;

@property (nonatomic,strong)MCBetModel *betModel;

/*
 * 奖金的总和
 */
@property (nonatomic,assign)float  allBouns;

@end


@implementation MCMMCNewViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self  updatePreferredContentSizeWithTraitCollection:self.traitCollection];
    
    [self setProperty];
    
    [self  createUI];
    
    [self loadData];
    
    
    
}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    _allBouns = 0;
    
}

-(void)setProperty{
    
    _allBouns=0;
    _allKaiJiangCount=_lianKaiCount;
    self.view.backgroundColor=[UIColor  whiteColor];
   
}


-(void)createUI{
    
    _btn_ball1=[[UIButton alloc]init];
    [self setBallBtn:_btn_ball1 andIndex:0];
    
    _btn_ball2=[[UIButton alloc]init];
    [self setBallBtn:_btn_ball2 andIndex:1];

    
    _btn_ball3=[[UIButton alloc]init];
    [self setBallBtn:_btn_ball3 andIndex:2];

    
    _btn_ball4=[[UIButton alloc]init];
    [self setBallBtn:_btn_ball4 andIndex:3];

    
    _btn_ball5=[[UIButton alloc]init];
    [self setBallBtn:_btn_ball5 andIndex:4];

    
    
    UILabel * lab_title=[[UILabel alloc]init];
    lab_title.text=@"第1次开奖";
    lab_title.font=[UIFont systemFontOfSize:13];
    lab_title.textColor=RGB(100, 100, 100);
    [self.view addSubview:lab_title];
    lab_title.textAlignment=NSTextAlignmentCenter;
    lab_title.frame=CGRectMake(0, 75, 275, 20);
    _lab_title=lab_title;
    
    UIButton * btn_Stop=[[UIButton alloc]init];
    btn_Stop.backgroundColor=RGB(48, 127, 207);
    [btn_Stop setTitle:@"停止开奖" forState:UIControlStateNormal];
    [btn_Stop.titleLabel setTextColor:[UIColor whiteColor]];
    btn_Stop.layer.cornerRadius=5;
    [btn_Stop addTarget:self action:@selector(stopKaiJiang) forControlEvents:UIControlEventTouchUpInside];
    btn_Stop.clipsToBounds=YES;
    [self.view addSubview:btn_Stop];
    btn_Stop.frame=CGRectMake((275-142)/2.0, 100, 142, 30);
    _btn_Stop=btn_Stop;
    
    
    UILabel * lab_back=[[UILabel alloc]init];
    [self.view addSubview:lab_back];
    lab_back.frame=CGRectMake(10, 100, 275-20, 40);
    lab_back.numberOfLines=0;
    lab_back.textColor=RGB(100, 100, 100);
    lab_back.font=[UIFont systemFontOfSize:12];
    lab_back.textAlignment=NSTextAlignmentLeft;
    lab_back.text=@"很遗憾未中奖，祝您下次好运！\n5秒后返回投注页面...";
    lab_back.hidden=YES;
    _lab_back=lab_back;
    
    
    
}

-(void)setBallBtn:(UIButton*)btn andIndex:(int)index{
    int padding = (275-28*5-10*4)/2.0;
    int width_ball=28;
    
    [btn setBackgroundImage:[UIImage imageNamed:@"goucai_number_selected"] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn setTitle:@"0" forState:UIControlStateNormal];
    [btn.titleLabel setTextColor:[UIColor whiteColor]];
    btn.frame=CGRectMake(padding+index*(width_ball+10), 20, width_ball, width_ball);
    btn.backgroundColor=[UIColor clearColor];
}


#pragma mark-添加定时器
-(void)add_timer{
    _timer = [NSTimer timerWithTimeInterval:0.05 target:self selector:@selector(timerAction_KaiJiang) userInfo:nil repeats:YES];
    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    [currentRunLoop addTimer:_timer forMode:NSRunLoopCommonModes];

}

-(void)timerAction_KaiJiang{
    

    [_btn_ball1 setTitle:[NSString stringWithFormat:@"%d",arc4random()%10] forState:UIControlStateNormal];
    
    [_btn_ball2 setTitle:[NSString stringWithFormat:@"%d",arc4random()%10] forState:UIControlStateNormal];
    
    [_btn_ball3 setTitle:[NSString stringWithFormat:@"%d",arc4random()%10] forState:UIControlStateNormal];
    
    [_btn_ball4 setTitle:[NSString stringWithFormat:@"%d",arc4random()%10] forState:UIControlStateNormal];
    
    [_btn_ball5 setTitle:[NSString stringWithFormat:@"%d",arc4random()%10] forState:UIControlStateNormal];

    

}



- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    
    [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
    [self updatePreferredContentSizeWithTraitCollection:newCollection];
    
}

#pragma mark-更新高度
- (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection{
    
    self.preferredContentSize = CGSizeMake(300, 150);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadData{
    
    _lab_back.hidden=YES;
    _btn_Stop.hidden=NO;
    _lab_title.hidden=NO;
    
    _lab_title.text=[NSString stringWithFormat:@"第%d次开奖",_allKaiJiangCount-_lianKaiCount+1];
    
    [self  add_timer];
    
    /*
     * 网络请求
     */
//    __weak __typeof__ (self) wself = self;
    MCBetModel *betModel = [[MCBetModel alloc] initWithDic:_dic];
    self.betModel=betModel;
    [betModel refreashDataAndShow];
    
    betModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {

        [SVProgressHUD showErrorWithStatus:@"服务器繁忙,请稍后重试！"];
        [self stopKaiJiang];
        
    };
    betModel.callBackSuccessBlock = ^(id manager) {
        //        AwardMoney = 0;
        //        DrawResult = "3,8,2,3,8";
        //        Isaward = 0;
        //        IssueNumber = 20170810150536179;
        //        OrderID = BGIcwirZwQkPJLc;
        
        
//        NSLog(@"------%@",manager[@"DrawResult"]);
        NSArray * arr=manager;
        [self performSelector:@selector(ShowKaiJiang:) withObject:arr /*可传任意类型参数*/ afterDelay:2.0];
       
        
    };
    --_lianKaiCount;
    
}


-(void)ShowKaiJiang:(NSArray *)arr{
    
    for (NSDictionary *manager in arr) {
        if ([manager[@"Isaward"] intValue]==1) {
            
            _allBouns=_allBouns+[manager[@"AwardMoney"] floatValue];
            
        }else{
            
            
        }
        
        
    }
    NSDictionary *manager=arr[0];
    
    NSLog(@"--haoMa-%@--",manager[@"DrawResult"]);
    
    NSArray * arrNum=[manager[@"DrawResult"] componentsSeparatedByString:@","];
    
    [_timer invalidate];
    _timer = nil;
    
    [_btn_ball1 setTitle:arrNum[0] forState:UIControlStateNormal];
    
    [_btn_ball2 setTitle:arrNum[1] forState:UIControlStateNormal];
    
    [_btn_ball3 setTitle:arrNum[2] forState:UIControlStateNormal];
    
    [_btn_ball4 setTitle:arrNum[3] forState:UIControlStateNormal];
    
    [_btn_ball5 setTitle:arrNum[4] forState:UIControlStateNormal];
    
    if (_lianKaiCount>0) {
        
        
        [self performSelector:@selector(loadData) withObject:nil/*可传任意类型参数*/ afterDelay:2.0];
        
    }else{
        
       
        
        
        
        [self backToPickNumber];
        
    }
}
#pragma mark-停止开奖
-(void)stopKaiJiang{
    
    _lianKaiCount=0;
    [_timer invalidate];
    _timer = nil;
    
    [self backToPickNumber];

//    [self dismissViewControllerAnimated:YES completion:NULL];
    
}

-(void)backToPickNumber{
    
    _lab_back.hidden=NO;
    _btn_Stop.hidden=YES;
    _lab_title.hidden=YES;
    
    if (_allBouns > 0) {
        
        _lab_back.text=[NSString stringWithFormat:@"恭喜你，中奖啦！获得奖金%@元。\n5秒后返回投注页面...",GetRealFloatNum(_allBouns)];
        
    }else{
        
        _lab_back.text=@"很遗憾未中奖，祝您下次好运！\n5秒后返回投注页面...";
        
    }
    
    
    _lianKaiCount=0;
    [_timer invalidate];
    _timer = nil;
    
    MCUserMoneyModel * userMoneyModel=[MCUserMoneyModel sharedMCUserMoneyModel];
    userMoneyModel.isNeedLoad=YES;
    
    [self performSelector:@selector(GobackToPickNumber) withObject:nil/*可传任意类型参数*/ afterDelay:5.0];
}

-(void)GobackToPickNumber{
    
    [self dismissViewControllerAnimated:NO completion:^{
        
        MCPaySelectedLotteryTableViewController * vc=(MCPaySelectedLotteryTableViewController*)[UIView MCcurrentViewController];
        [vc emptyShoppingCart];
        
        [vc.navigationController popViewControllerAnimated:YES];
        
    }];
    

}
@end




























